package com.maori.languageapp.curate.word;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;
import com.maori.languageapp.R;
import com.maori.languageapp.consts.ImageLoader;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.media.AudioManager;
import android.os.AsyncTask;

public class Maori_Curate_Word_Adapter extends BaseAdapter {
	// Declare Variables
	Context context;
	LayoutInflater inflater;
	ArrayList<HashMap<String, String>> data;
	ImageLoader imageLoader;
	String DisplayImageSource,NodeCurateState,NodeApprovalCount,NodeRejectedCount;
	private LayoutInflater mInflater;
	HashMap<String, String> resultp = new HashMap<String, String>();
	String BookMarkState,UserNodeLikedState,node_dif_audio_url;
	private MediaPlayer mediaPlayer;
	HttpParams httpParameters = new BasicHttpParams();
	int timeoutConnection = 5000;
	JSONArray jsonarray;
	InputStream is = null;
	StringBuilder sb = null;
	String result;
	String NID,USERID;

	public Maori_Curate_Word_Adapter(Context context,
			ArrayList<HashMap<String, String>> arraylist) {
		this.context = context;
		data = arraylist;
		imageLoader = new ImageLoader(context);
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// Declare Variables
		final ViewHolder holder;
		// Declare Variables
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.maori_curate_main_list_raw, null);
			holder = new ViewHolder();
			holder.maori_title = (TextView) convertView
					.findViewById(R.id.discover_maori_title);
			holder.english_title = (TextView) convertView
					.findViewById(R.id.discover_english_title);
			holder.Synonims = (Button) convertView
					.findViewById(R.id.textrawtxta);
			holder.Audios = (Button) convertView.findViewById(R.id.textrawtxtb);
			holder.Images = (Button) convertView.findViewById(R.id.textrawtxtc);
			holder.Videos = (Button) convertView.findViewById(R.id.textrawtxtd);
			holder.Likes = (Button) convertView.findViewById(R.id.textrawtxte);
			holder.discover_bookmark = (Button) convertView
					.findViewById(R.id.discover_bookmark);
			holder.playdefaultaudio = (Button) convertView
					.findViewById(R.id.playdefaultaudio);
			// Locate the ImageView in listview_item.xml
			holder.flag = (ImageView) convertView
					.findViewById(R.id.mem_discover_img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		resultp = data.get(position);

		holder.maori_title.setText(resultp.get(Maori_Curate_Word.getWord()));
		holder.english_title
				.setText(resultp.get(Maori_Curate_Word.getWordb()));

		// Print Bottom Raw Count details
		holder.Synonims.setText(resultp.get(Maori_Curate_Word
				.getTotalsSynonim()));
		holder.Audios
				.setText(resultp.get(Maori_Curate_Word.getTotalAudios()));
		holder.Images
				.setText(resultp.get(Maori_Curate_Word.getTotalImages()));
		holder.Videos
				.setText(resultp.get(Maori_Curate_Word.getTotalVideos()));
		holder.Likes.setText(resultp.get(Maori_Curate_Word.getTotalsLikes()));
		holder.playdefaultaudio.setText(resultp.get(Maori_Curate_Word
				.getNode_dif_audio_url()));

		String NodeMainImage = resultp.get(Maori_Curate_Word.getM_Source());
		if (NodeMainImage.equals("404")) {
			DisplayImageSource = "http://langapp.mirzabros.com/images/placeholder.png";
		} else {
			DisplayImageSource = resultp.get(Maori_Curate_Word.getM_Source());
		}

		imageLoader.DisplayImage(DisplayImageSource, holder.flag);

		UserNodeLikedState = resultp.get(Maori_Curate_Word.getUserLikedForNode());
		BookMarkState = resultp
				.get(Maori_Curate_Word.getUserNodeBookMarked());
		node_dif_audio_url = resultp.get(Maori_Curate_Word
				.getNode_dif_audio_url());
		
		//**************Curate Handling*******************
		NodeCurateState = resultp.get(Maori_Curate_Word.getNodeCurateState());
		NodeApprovalCount = resultp.get(Maori_Curate_Word.getNodeApprovalCount());
		NodeRejectedCount = resultp.get(Maori_Curate_Word.getNodeRejectedCount()); 
		
		int NodeApprovalCountVal = Integer.parseInt(NodeApprovalCount);
		int NodeRejectedCountVal = Integer.parseInt(NodeRejectedCount);
		
		if(NodeApprovalCountVal == 1 && NodeRejectedCountVal < 2){
			holder.discover_bookmark.setBackgroundResource(R.drawable.ic_star_pending);
		}
		else if(NodeRejectedCountVal == 1){
			holder.discover_bookmark.setBackgroundResource(R.drawable.ic_star_pending);
		}
		else if(NodeApprovalCountVal >= 2){
			holder.discover_bookmark.setBackgroundResource(R.drawable.ic_star_active);
		}
		else if(NodeRejectedCountVal >= 2){
			holder.discover_bookmark.setBackgroundResource(R.drawable.ic_star_reject);
		}
		
		
		if(!UserNodeLikedState.equals("0")){
			holder.Likes.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_like_active_raw_count, 0);
		}

		// Check default audio available
		if (!node_dif_audio_url.equals("202")) {
			holder.playdefaultaudio.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_play_h, 0);
			holder.playdefaultaudio.setEnabled(true);
			// playdefaultaudio.setCompoundDrawablesWithIntrinsicBounds(left,
			// top, right, bottom)
		}


		// Capture button clicks on ListView items
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// Get the position from the results
				HashMap<String, String> resultp = new HashMap<String, String>();
				resultp = data.get(position);
				// Send single item click data to SingleItemView Class
				Intent intent = new Intent(context,
						Maori_Curate_Word_Details.class);
				intent.putExtra("word",
						resultp.get(Maori_Curate_Word.getWord()));
				intent.putExtra("maori",
						resultp.get(Maori_Curate_Word.getWordb()));
				intent.putExtra("nid",
						resultp.get(Maori_Curate_Word.getNID()));
				intent.putExtra("Source",
						resultp.get(Maori_Curate_Word.getM_Source()));
				intent.putExtra("MID",
						resultp.get(Maori_Curate_Word.getMID()));
				intent.putExtra("first_name",
						resultp.get(Maori_Curate_Word.getFirst_name()));
				intent.putExtra("last_name",
						resultp.get(Maori_Curate_Word.getLast_name()));
				intent.putExtra("tribe",
						resultp.get(Maori_Curate_Word.getTribe()));
				intent.putExtra("userimage",
						resultp.get(Maori_Curate_Word.getUserimage()));
				intent.putExtra("TotalsSynonim",
						resultp.get(Maori_Curate_Word.getTotalsSynonim()));
				intent.putExtra("TotalAudios",
						resultp.get(Maori_Curate_Word.getTotalAudios()));
				intent.putExtra("TotalImages",
						resultp.get(Maori_Curate_Word.getTotalImages()));
				intent.putExtra("TotalVideos",
						resultp.get(Maori_Curate_Word.getTotalVideos()));
				intent.putExtra("TotalsLikes",
						resultp.get(Maori_Curate_Word.getTotalsLikes()));
				context.startActivity(intent);
				((Maori_Curate_Word)context).finish();

			}
		});

		return convertView;
	}

	static class ViewHolder {
		TextView maori_title;
		TextView english_title;
		Button Synonims;
		Button Audios;
		Button Images;
		Button Videos;
		Button Likes;
		Button discover_bookmark;
		Button playdefaultaudio;
		ImageView flag;
	}
}
